package com.lagou.filter;

import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;


@Activate(group = {CommonConstants.CONSUMER,CommonConstants.PROVIDER})
public class DubboInvokeFilter   implements Filter {
    static List methodA=new ArrayList();
    static List methodB=new ArrayList();
    static List methodC=new ArrayList();
    static {
        TaskTwo();
    }

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        long   startTime  = System.currentTimeMillis();
        try {
            // 执行方法
            System.out.println(invocation.getMethodName());
            return  invoker.invoke(invocation);
        } finally {
            System.out.println("invoke time:"+(System.currentTimeMillis()-startTime) + "毫秒");
            //添加耗时数据
            if ("methodA".equals(invocation.getMethodName())){
                methodA.add(System.currentTimeMillis()-startTime);
            }else if ("methodB".equals(invocation.getMethodName())){
                methodB.add(System.currentTimeMillis()-startTime);
            }else if ("methodC".equals(invocation.getMethodName())){
                methodC.add(System.currentTimeMillis()-startTime);
            }
        }

    }

    public static void TaskTwo() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("111");
                double methodA90=methodA.size()*0.9;
                double methodA99=methodA.size()*0.99;
                System.out.println("methodA TP90:"+methodA90+"methodA TP99:"+methodA99);


                double methodB90=methodB.size()*0.9;
                double methodB99=methodB.size()*0.99;
                System.out.println("methodB TP90:"+methodB90+"methodB TP99:"+methodB99);


                double methodC90=methodC.size()*0.9;
                double methodC99=methodC.size()*0.99;
                System.out.println("methodC TP90:"+methodC90+"methodC TP99:"+methodC99);

                System.out.println(methodA);
                System.out.println(methodB);
                System.out.println(methodC);
            }
        }, 0, 5000);// 2000=开始延迟时间 500=间隔时间
    }

    public static void TaskMinute() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                methodA.clear();
            }
        }, 0, 1000*60);// 一分钟清空一次
    }


    public static void main(String[] args) {
        System.out.println(100*0.5);
    }

}
